home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
clang
/
nn.zip
/
ANSWER.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-31
|
11KB
|
545 lines
#include "config.h"
#include "news.h"
#include "term.h"
#include "keymap.h"
extern char *temp_file;
export char *default_distribution = NULL;
export char *extra_mail_headers = NULL;
export char *extra_news_headers = NULL;
export char *mail_record = NULL;
export char *news_record = NULL;
export char *mail_script = NULL;
export char *news_script = NULL;
export int nn_re_style = 0; /* use Re^n: in replies */
export int include_art_id = 0;
#define INCL_MARK_SIZE 10
export char included_mark[INCL_MARK_SIZE + 1] = ">";
static int ed_line;
answer(ah, command, incl)
article_header *ah;
int command;
int incl; /* <0: ask, 0: don't include, >0: include article */
{
register FILE *t, *art;
char *pgm, *first_action, *record_file;
int edit_message;
char *str, *script;
news_header_buffer nhbuf, dhbuf;
first_action = "edit";
edit_message = 1;
if (incl < 0) {
prompt("Include original article? ");
if ((incl = yes(0)) < 0) return 0;
}
art = NULL;
if (ah && ah->a_group) init_group(ah->a_group);
if (incl || command != K_MAIL_OR_FORWARD) {
int open_modes;
open_modes = FILL_NEWS_HEADER | GET_ALL_FIELDS | SKIP_HEADER;
if (ah->flag & A_DIGEST) open_modes |= FILL_DIGEST_HEADER;
art = open_news_article(ah, open_modes, nhbuf, dhbuf);
if (art == NULL) {
msg("Can't find original article");
return 0;
}
if (ah->flag & A_DIGEST) {
if (digest.dg_from)
news.ng_path = news.ng_from = digest.dg_from;
if (digest.dg_subj)
news.ng_subj = digest.dg_subj;
}
} else
ah = NULL;
/* build header */
new_temp_file();
if ((t = open_file(temp_file, OPEN_CREATE)) == NULL) {
msg("Can't create %s", temp_file);
return 0;
}
ed_line = 0;
follow_to_poster:
record_file = mail_record;
script = mail_script;
if (command == K_REPLY) {
pgm = "reply";
ah->flag |= A_ST_REPLY;
if (reply_to(t, news.ng_reply) ||
reply_to(t, news.ng_from) ||
reply_to(t, news.ng_path)) goto alt0;
if (to_line(t, news.ng_reply)) goto alt1;
if (to_line(t, news.ng_from)) goto alt2;
if (to_line(t, news.ng_path)) goto alt3;
goto err;
alt0:
alt_to_line(t, news.ng_reply);
alt1:
alt_to_line(t, news.ng_from);
alt2:
alt_to_line(t, news.ng_path);
alt3:
if (news.ng_subj)
subj_line(t, ah->replies, ah->subject, (char *)NULL);
else
subj_line(t, 0, current_group->group_name, "Your Article in");
ng_line(t, 0);
ref_line(t);
end_header(t, extra_mail_headers);
if (incl) {
fprintf(t, "In %s you write:\n", current_group->group_name);
ed_line++;
}
}
if (command == K_FOLLOW_UP) {
if (news.ng_follow && strcmp(news.ng_follow, "poster") == 0) {
command = K_REPLY;
msg("Followup by reply to poster");
goto follow_to_poster;
}
pgm = "follow";
record_file = news_record;
script = news_script;
ah->flag |= A_ST_FOLLOW;
ng_line(t, 1);
if (news.ng_subj)
subj_line(t, ah->replies, ah->subject, (char *)NULL);
else
if (!subj_line(t, 0, news.ng_from, "Babble from"))
if (!subj_line(t, 0, news.ng_ident, "Article")) {
prompt("Subject: ");
str = get_s(NONE, NONE, NONE, NO_COMPLETION);
if (str == NULL) goto err;
subj_line(t, -1, str, (char *)NULL);
}
if (news.ng_keyw) {
fprintf(t, "Keywords: %s\n", news.ng_keyw);
ed_line++;
}
if (news.ng_dist) {
fprintf(t, "Distribution: %s\n", news.ng_dist);
ed_line++;
}
ref_line(t);
end_header(t, extra_news_headers);
if (incl) {
if (news.ng_from) {
if (include_art_id && news.ng_ident)
fprintf(t, "In %s %s ",
ah->flag & A_DIGEST ? "digest" : "article",
news.ng_ident);
fprintf(t, "%s writes:\n", news.ng_from);
ed_line++;
} else
if (news.ng_ident) {
fprintf(t, "In %s %s:\n",
ah->flag & A_DIGEST ? "digest" : "article",
news.ng_ident);
ed_line++;
}
}
}
if (command == K_MAIL_OR_FORWARD) {
pgm = incl ? "forward" : "mail";
m3_again:
prompt("To: ");
str = get_s(user_name(), NONE, NONE, NO_COMPLETION);
if (str == NULL) goto close_t;
if (*str == NUL) str = user_name();
if (*str == '?') goto m3_again;
if (strcmp(str, user_name()) == 0)
record_file = NULL; /* we will get this anyway,
there is so no need to save it */
/* if (reply_to(t, str)) { alt_to_line(t, str); } else */
to_line(t, str);
do {
prompt("Subject: ");
str = get_s(incl ? ah->subject : NONE, NONE, NONE, NO_COMPLETION);
if (str == NULL) goto close_t;
if (*str == NUL && incl) str = ah->subject;
} while (*str == NUL);
subj_line(t, -1, str, (char *)NULL);
end_header(t, extra_mail_headers);
if (incl) {
prompt("\1Edit\1 forwarded message? ");
if ((edit_message = yes(0)) < 0) goto close_t;
if (!edit_message) {
first_action = "send";
fseek(art, ah->hpos, 0);
}
}
}
/* empty line terminates header */
fputc(NL, t);
ed_line++;
prompt("\1WAIT\1");
if (incl) {
register c, prevnl = 1;
while ((c = getc(art)) != EOF) {
if (c == NL) {
putc(c, t);
if (ftell(art) >= ah->lpos) break;
prevnl++;
continue;
}
if (prevnl) {
if (command != K_MAIL_OR_FORWARD || ftell(art) < ah->fpos)
fputs(included_mark, t);
prevnl = 0;
}
putc(c, t);
}
} else {
putc(NL, t);
ed_line++;
}
fclose(t);
if (art) fclose(art);
aux_sh(script, pgm, first_action, record_file,
command == K_FOLLOW_UP ? "Article not posted" : "Mail not sent");
return edit_message;
err:
msg("Can't build header for %s",
command != K_FOLLOW_UP ? "letter" : "article");
close_t:
fclose(t);
unlink(temp_file);
if (art) fclose(art);
return 0;
}
cancel(ah)
article_header *ah;
{
news_header_buffer nhbuf;
FILE *f;
if (ah->a_group) init_group(ah->a_group);
if (ah->flag & A_DIGEST) {
fputs("\rCancel entire digest ? ", stdout); clrline();
if (yes(1) > 0)
ah->flag &= ~A_DIGEST;
else {
msg("Can only cancel entire digests (yet?)");
return 2;
}
} else {
prompt("Confirm cancel: '%s: %.30s'",
ah->sender ? ah->sender : "",
ah->subject ? ah->subject : "");
if (yes(1) <= 0) return 1;
}
f = open_news_article(ah, FILL_NEWS_HEADER|GET_ALL_FIELDS, nhbuf, (char *)NULL);
if (f == NULL) {
msg("Article not found");
return 2;
}
fclose(f);
printf("\rCancelling article %s in group %s",
news.ng_ident, current_group->group_name);
clrline();
ed_line = -1;
if (aux_sh((char *)NULL, "cancel",
news.ng_ident, current_group->group_name, "Not canceled"))
return -1;
return 0;
}
post_menu()
{
FILE *t;
char *str, *tail;
char group_name[FILENAME], subject[FILENAME],
distribution[FILENAME], keywords[FILENAME];
extern group_completion();
group_name[0] = NUL;
again_group:
prompt("\1POST to group\1 ");
str = get_s(current_group ? current_group->group_name : NONE,
group_name, NONE, group_completion);
if (str == NULL || *str == NUL) return 0;
strcpy(group_name, str);
for (str = group_name; str; str = tail) {
tail = strchr(str, ',');
if (tail) *tail = NUL;
if (lookup(str) == NULL) {
msg("unknown group: %s", str);
*str = NUL;
goto again_group;
}
if (tail) *tail++ = ',';
}
prompt("Subject: ");
str = get_s(NONE, NONE, NONE, NO_COMPLETION);
if (str == NULL || *str == NUL) return 0;
strcpy(subject, str);
prompt("Keywords: ");
str = get_s(NONE, NONE, NONE, NO_COMPLETION);
if (str == NULL) return 0;
strcpy(keywords, str);
if (default_distribution != NULL)
strcpy(distribution, default_distribution);
else {
strcpy(distribution, group_name);
if (str = strchr(distribution, '.')) *str = NUL;
}
prompt("\1Distribution\1 (default '%s') ", distribution);
str = get_s(NONE, NONE, NONE, NO_COMPLETION);
if (str == NULL) return 0;
if (*str) strcpy(distribution, str);
new_temp_file();
if ((t = open_file(temp_file, OPEN_CREATE)) == NULL) {
msg("Can't create %s", temp_file);
return 0;
}
prompt("\1WAIT\1");
ed_line = 5;
fprintf(t, "Newsgroups: %s\n", group_name);
fprintf(t, "Distribution: %s\n", distribution);
fprintf(t, "Subject: %s\n", subject);
if (*keywords) {
fprintf(t, "Keywords: %s\n", keywords);
ed_line++;
}
end_header(t, extra_news_headers);
fputc(NL, t);
fclose(t);
aux_sh(news_script, "post", "edit", news_record, "Article not posted");
return 1;
}
static subj_line(t, re, subj, prefix)
FILE *t;
int re;
char *subj, *prefix;
{
if (subj == NULL) return 0;
fputs("Subject: ", t);
if (re == 0 || !nn_re_style)
fputs("Re: ", t);
else if (re > 0)
fprintf(t, "Re^%d: ", re + 1);
if (prefix) {
fputs(prefix, t);
fputc(' ', t);
}
fputs(subj, t);
fputc(NL, t);
ed_line++;
return 1;
}
static ng_line(t, use_follow)
FILE *t;
int use_follow;
{
fprintf(t, "Newsgroups: %s\n",
use_follow && news.ng_follow ? news.ng_follow : news.ng_groups);
ed_line++;
}
static ref_line(t)
FILE *t;
{
if (news.ng_ref == NULL && news.ng_ident == NULL) return;
fputs("References:", t);
if (news.ng_ref) fprintf(t, " %s", news.ng_ref);
if (news.ng_ident) fprintf(t, " %s", news.ng_ident);
putc(NL, t);
ed_line++;
}
static to_line(t, to)
FILE *t;
char *to;
{
if (to == NULL) return 0;
fprintf(t, "To: %s\n", to);
ed_line++;
return 1;
}
static alt_to_line(t, to)
FILE *t;
char *to;
{
if (to == NULL) return;
fprintf(t, "Orig-To: %s\n", to);
ed_line++;
}
static end_header(t, extra_headers)
FILE *t;
register char *extra_headers;
{
if (extra_headers != NULL && *extra_headers != NUL) {
while (*extra_headers != NUL) {
if (*extra_headers == ';') {
if (*++extra_headers == NUL) break;
fputc(NL, t);
ed_line++;
} else
fputc(*extra_headers++, t);
}
fputc(NL, t);
ed_line++;
}
fputc(NL, t);
ed_line++;
}
static reply_to(t, address)
FILE *t;
char *address;
{
char route[512];
if (address == NULL) return 0;
if (reroute(route, address)) {
to_line(t, route);
return 1;
}
return 0;
}
/*
* invoke aux shell script with suitable arguments
*
* WARNING: record may be NULL, soit must be the last argument!!
*/
static aux_sh(script, prog, action, record, not_sent)
char *script, *prog, *action, *record, *not_sent;
{
char *args[8];
char number[10];
register char **ap = args;
time_t start_t;
*ap++ = "nnaux";
*ap++ = script != NULL ? script : relative(lib_directory, "aux");
*ap++ = prog;
if (ed_line >= 0) { /* not cancel */
sprintf(number, "%d", ed_line);
*ap++ = temp_file;
*ap++ = number;
}
*ap++ = action; /* article id for cancel */
*ap++ = record; /* group name for cancel */
*ap++ = NULL;
#ifdef STATISTICS
time(&start_t);
#endif
if (execute(SHELL, args)) {
prompt_line = -1;
prompt("\1%s\1", not_sent);
sleep(1);
return 1;
}
#ifdef STATISTICS
tick_usage((time_t *)NULL, &start_t);
#endif
return 0;
}